package com.sdp.irrigation.config;

import com.sdp.core.bizc.shiro.ShiroAuthFilter;
import com.sdp.core.bizc.shiro.ShiroRelam;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

	@Bean
	public ShiroRelam shiroRelam() {
        return new ShiroRelam();
    }
    @Bean
    public SecurityManager  securityManager() {
    	DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRelam());
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
    	ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
    	shiroFilterFactoryBean.setSecurityManager(securityManager);

    	Map<String, Filter> filters = new HashMap<>();
    	filters.put("authFilter", new ShiroAuthFilter());
    	shiroFilterFactoryBean.setFilters(filters);

    	LinkedHashMap<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();
    	filterChainDefinitionMap.put("/LoginController/login", "anon");
    	filterChainDefinitionMap.put("/AppLoginController/login", "anon");
    	filterChainDefinitionMap.put("/LoginController/generateKey", "anon");
    	filterChainDefinitionMap.put("/LoginController/refreshToken", "anon");
    	filterChainDefinitionMap.put("/SysUploadController/**", "anon");
    	filterChainDefinitionMap.put("/MobileController/**", "anon");
    	filterChainDefinitionMap.put("/CommandDownlinkController/**", "anon");
    	filterChainDefinitionMap.put("/AppExcludeController/**", "anon");
    	
    	filterChainDefinitionMap.put("/**/video/**", "anon");
    	filterChainDefinitionMap.put("/doc.html", "anon");
		filterChainDefinitionMap.put("/**/*.js", "anon");
		filterChainDefinitionMap.put("/**/*.css", "anon");
		filterChainDefinitionMap.put("/**/*.html", "anon");
		filterChainDefinitionMap.put("/**/*.svg", "anon");
		filterChainDefinitionMap.put("/**/*.pdf", "anon");
		filterChainDefinitionMap.put("/**/*.jpg", "anon");
		filterChainDefinitionMap.put("/**/*.png", "anon");
		filterChainDefinitionMap.put("/**/*.ico", "anon");
		filterChainDefinitionMap.put("/**/*.jpeg", "anon");
		filterChainDefinitionMap.put("/websocket/**", "anon");
    	//其他所有登陆走自定义的FILTER
    	filterChainDefinitionMap.put("/**", "authFilter");

    	shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }

}
